package com.yidu.ladder.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.base.dao.RouteDao;
import com.yidu.ladder.base.domain.Route;
import com.yidu.ladder.base.domain.RouteVo;
import com.yidu.ladder.base.domain.Subset;
import com.yidu.ladder.base.service.RouteService;

/**
 * 路线业务逻辑接口实现类
 * 
 * @author yy
 * @since 2019-12-2
 *
 */
@Service
public class RouteServiceImpl implements RouteService {

	/**
	 * 关联线路数据访问层
	 */
	@Autowired
	private RouteDao routeDao;

	public RouteDao getRouteDao() {
		return routeDao;
	}

	public void setRouteDao(RouteDao routeDao) {
		this.routeDao = routeDao;
	}

	/**
	 * 查询路线
	 */
	@Override
	public Map<String, Object> queryRouteByCriteriaWithPage(RouteVo routeVo) {
		// 计算分页
		int page = (routeVo.getPage() - 1) * routeVo.getLimit();
		routeVo.setPage(page);
		// 查询满足检索条件的线路
		List<RouteVo> routeList = routeDao.queryRouteByCriteriaWithPage(routeVo);
		// 统计线路总数
		int total = routeDao.countRouteByCriteria(routeVo);
		// 创建返回结果映射: {"total":"用户总数",rows:"指定页面的用户集合"}
		HashMap<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("code", 0);
		returnMap.put("msg", "");
		returnMap.put("count", total);
		returnMap.put("data", routeList);
		return returnMap;
	}

	/**
	 * 添加路线
	 */
	@Override
	public int addRoute(Route route) {
		//调用添加路线方法
		return routeDao.addRoute(route);
	}

	/**
	 * 修改路线
	 */
	@Override
	public boolean updateRoute(Route route) {
		//调用修改路线方法
		return routeDao.updateRoute(route) == 1 ? true : false;
	}

	/**
	 * 冻结或还原路线
	 */
	@Override
	public boolean frozenOrRecoveRoute(String routeId, String useable) {
		String[] routeIdArr = routeId.split(",");
		int totle = 0;
		for (String rId : routeIdArr) {
			// 创建一个对象
			Route route = new Route();
			// 赋值
			route.setRouteId(rId);
			route.setUseable(useable);
			// 调用修改线路方法
			int result = routeDao.updateRoute(route);
			if (result == 1) {
				totle++;
			}
		}
		return totle == routeIdArr.length ? true : false;
	}

	/**
	 * 给下拉框赋值
	 */
	@Override
	public List<Subset> selectAllRoute() {
		return routeDao.selectAllRoute();
	}

}
